<%@ page import="com.ws.ssmzongjie3.util.Res" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>编辑用户</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
        }
        .form-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            max-width: 500px;
            margin: 0 auto;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .form-group input, .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .form-group input[type="submit"] {
            background-color: #007bff;
            color: #fff;
            border: none;
            cursor: pointer;
        }
        .form-group input[type="submit"]:hover {
            background-color: #0056b3;
        }
    </style>
    <!-- 引入 jQuery -->
    <script src="${pageContext.request.contextPath}/bootstrap/js/jquery-2.1.0.min.js"></script>
</head>
<body>
    <div class="form-container">
        <h2>编辑用户</h2>
        <form action="${pageContext.request.contextPath}/houuser/update" method="post" enctype="multipart/form-data">
            <input type="hidden" name="acl_user_id" value="${user.acl_user_id}">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" value="${user.username}" required onblur="checkUsername()">
                <span class="error" id="usernameError" style="color: red; display: none;">用户名已存在，请重新输入</span>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password">
            </div>
            <div class="form-group">
                <label for="nickname">昵称</label>
                <input type="text" id="nickname" name="nickname" value="${user.nickname}">
            </div>
            <div class="form-group">
                <label for="acl_user_avatar">头像：</label>
                <input type="file" class="form-control" name="image" id="acl_user_avatar" accept="image/*">
                <!-- 图片预览区域 -->
                <div id="image-preview" style="margin-top: 10px;">
                    <c:if test="${not empty user.acl_user_avatar}">
                        <img src="<%=Res.fileUrl%>${user.acl_user_avatar}" width="100" height="100" alt="当前头像">
                    </c:if>
                </div>
            </div>

            <div class="form-group">
                <label for="sid">商家：</label>
                <select name="sid" id="sid">
                    <option value="0">请选择</option>
                    <c:forEach items="${businesses}" var="business">
                        <option value="${business.business_id}" <c:if test="${business.business_id == user.sid}">selected</c:if>>${business.business_name}</option>
                    </c:forEach>
               </select>


            <div class="form-group">
                <input type="submit" value="更新用户">
            </div>
        </form>
    </div>
</body>
</html>

<script>
    // 图片上传预览功能
    document.getElementById('acl_user_avatar').addEventListener('change', function(event) {
        var preview = document.getElementById('image-preview');
        preview.innerHTML = ''; // 清空之前的预览

        var files = event.target.files;
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            var reader = new FileReader();

            reader.onload = (function(file) {
                return function(e) {
                    var img = document.createElement('img');
                    img.src = e.target.result;
                    img.style.width = '100px';
                    img.style.height = '100px';
                    img.style.margin = '5px';
                    preview.appendChild(img);
                };
            })(file);

            reader.readAsDataURL(file);
        }
    });


    // 检查用户名是否重复
    function checkUsername() {
        const username = document.getElementById('username').value.trim();
        const userId = "${user.acl_user_id}"; // 获取当前用户的 ID

        $.ajax({
            url: '${pageContext.request.contextPath}/houuser/checkUsername',
            type: 'POST',
            data: { username: username, userId: userId },
            success: function(data) {
                if (data.exists) {
                    document.getElementById('usernameError').style.display = 'block';
                } else {
                    document.getElementById('usernameError').style.display = 'none';
                }
            },
            error: function(error) {
                console.error('Error:', error);
            }
        });
    }

    // 表单提交验证
    document.querySelector('form').addEventListener('submit', function(e) {
        if (document.getElementById('usernameError').style.display === 'block') {
            e.preventDefault();
            alert('用户名已存在，请重新输入');
        }
    });
</script>
